package com.lijian.netty.nio6;

import java.nio.ByteBuffer;

/**
 * @author lj02
 * @desc
 * @date 2018/11/28
 */
public class NioTest7 {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(15);

        // class java.nio.HeapByteBuffer
        System.out.println(byteBuffer.getClass());

        for (int i = 0; i < 10; i++) {
            byteBuffer.put((byte) i);
        }
        // 只读 buffer
        ByteBuffer readBuffer = byteBuffer.asReadOnlyBuffer();
        // class java.nio.HeapByteBuffer
        System.out.println(byteBuffer.getClass());
        // class java.nio.HeapByteBufferR
        System.out.println(readBuffer.getClass());

        byteBuffer.put(1, (byte) 9);
        byteBuffer.put((byte) 2);
        byteBuffer.put((byte) 3);

        readBuffer.flip();
        byteBuffer.flip();

        while (readBuffer.hasRemaining()) {
            System.out.println(readBuffer.get());
        }
        /**
         * 0
         * 9   // 注意此行
         * 2
         * 3
         * 4
         * 5
         * 6
         * 7
         * 8
         * 9
         */

        while (byteBuffer.hasRemaining()) {
            System.out.println(byteBuffer.get());
        }
        /**
         * 0
         * 9
         * 2
         * 3
         * 4
         * 5
         * 6
         * 7
         * 8
         * 9
         * 2  // 注意此行
         * 3  // 注意此行
         */
    }
}
